package com.jzr.bedside.ui.adapter

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jzr.bedside.R
import com.jzr.bedside.bean.CheckedListBean.Answer
import com.jzr.bedside.bean.TQuestionBean
import java.lang.Exception


class EvaluateChildrenApadter(
    data: MutableList<TQuestionBean.DataBean.OptionsBean>?,
    postion: Int, controlType: String, evaluateApadter: EvaluateApadter
) :
    BaseQuickAdapter<TQuestionBean.DataBean.OptionsBean, BaseViewHolder>(
        R.layout.item_evaluate_children,
        data
    ) {
    private var controlType: String? = null
    private var postion: Int? = null
    private var evaluateApadter: EvaluateApadter? = null

    init {
        this.postion = postion
        this.controlType = controlType
        this.evaluateApadter = evaluateApadter
    }

    override fun convert(
        helper: BaseViewHolder?,
        item: TQuestionBean.DataBean.OptionsBean?
    ) {
        try {
            helper?.setText(R.id.tv_name, item?.name)

            if (item?.isCheck != null && item.isCheck) {
                val answer = Answer()
                answer.answer = item.name
                answer.id = item.optionId
                helper?.getView<View>(R.id.iv_check)
                   ?.setBackgroundResource(R.mipmap.checkboxp)
                evaluateApadter?.addCheckedList(answer, postion!!)
            } else {
                helper!!.getView<View>(R.id.iv_check)
                    .setBackgroundResource(R.mipmap.checkboxn)
            }

            helper?.itemView?.setOnClickListener {
                if (item?.isCheck != null && item.isCheck) {
                    val answer = Answer()
                    answer.answer = item.name
                    answer.id = item.optionId
                    item.isCheck = false
                    helper.getView<View>(R.id.iv_check)
                        .setBackgroundResource(R.mipmap.checkboxn)
                    if (postion != null) {
                        evaluateApadter?.removeCheckedList(answer, postion!!)
                    }

                } else {
                    if (controlType == "radio") {   // 单选
                        for (i in data.indices) {
                            val answer = Answer()
                            answer.answer = data[i].name
                            answer.id = data[i].optionId
                            data[i].isCheck = false
                            if (postion != null) {
                                evaluateApadter?.removeCheckedList(answer, postion!!)
                            }
                        }
                        item?.isCheck = true
                    } else {
                        val answer = Answer()
                        answer.answer = item?.name
                        answer.id = item?.optionId!!
                        item.isCheck = true
                        if (postion != null) {
                            evaluateApadter?.removeCheckedList(answer, postion!!)
                        }
                        helper.getView<View>(R.id.iv_check)
                            .setBackgroundResource(R.mipmap.checkboxp)
                    }
                }
                notifyDataSetChanged()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
}